﻿@using CnGalWebSite.DrawingBed.Models.DataModels;
@typeparam TValue where TValue : BaseImageEditModel, new()


@inject IHttpService _httpService


<CnGalWebSite.Components.Images.UploadButton Multiple Text="上传图片到图床" Icon="IconType.CloudUpload" OnImageUploaded="AddImage" ShowPreview="false" />
 @if (Model.Count != 0)
{
    <MRow>
        @foreach (var item in Model.OrderByDescending(s => s.Priority))
        {
            <MCol Cols="6" Md="4" Lg="3" Xl="2">
                <img src="@item.Image" alt="@item.Note" class="w-100 mb-3">
                <div class="mb-4" style="display: flex;justify-content: space-between;">
                    <CnGalWebSite.Components.Buttons.MasaButton Small Rounded Color="secondary" Text="编辑" Icon="@IconType.Edit.ToIconString()" OnClick="@(()=> EditImage(item))" />
                    <CnGalWebSite.Components.Buttons.MasaButton Small Rounded Color="primary" Text="删除" Icon="@IconType.Delete.ToIconString()" OnClick="@(()=> DeleteImage(item))" />
                </div>
            </MCol>
        }
    </MRow>
}

@if (_model != null)
{
    <MDialog @bind-Value="_editDialog"
             MaxWidth="800">
        <MDCard>
            <MCardTitle>
                编辑图片
            </MCardTitle>
            <MCardText>
                <MRow>
                    <MCol Cols="12" Md="6">
                        <CnGalWebSite.Components.Images.UploadButton @bind-ImagePath="@_model.Image" Type="ImageAspectType.None" Text="上传图片" Class="mb-4"/>
                    </MCol>
                    <MCol Cols="12">
                    </MCol>
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="_model.Note" Label="备注" />
                    </MCol>
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="_model.Priority" Label="优先级" />
                    </MCol>
                    @if (ItemContent!=null)
                    {
                          @ItemContent(_model)
                    }
                  
                </MRow>

            </MCardText>
        </MDCard>
    </MDialog>
}


@code {
    [Parameter]
    public List<TValue> Model { get; set; } = new List<TValue>();
    [Parameter]
    public RenderFragment<TValue> ItemContent { get; set; }

    private bool _editDialog;
    TValue _model;


    public void DeleteImage(TValue model)
    {
        Model.Remove(model);
        StateHasChanged();
    }
    public void EditImage(TValue model)
    {
        _editDialog = true;
        _model = model;
        StateHasChanged();
    }

    public void AddImage(string url)
    {
        Model.Add(new TValue
            {
                Image = url
            });
        StateHasChanged();
    }
}
